package com.songqintuan.platform.util;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;
import java.sql.Timestamp;

import com.songqintuan.common.util.DateTimeUtil;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.SystemUser;
import com.songqintuan.platform.common.constant.PlatformManageBeanConstants;
import com.songqintuan.platform.user.CurrentUserBean;

public class EntityDataUtil<T,PK> {
	
	public void setModifyPropertyValue(T obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException{
		try {
		Class<? extends Object> objClass = obj.getClass();
		@SuppressWarnings("unchecked")
		Method objMethod = objClass.getMethod("setModifyTime", new Class[]{Timestamp.class});
		Timestamp nowTime = DateTimeUtil.getCurrentTimestamp();
		objMethod.invoke(obj, new Object[]{nowTime});
		//set update by info
		CurrentUserBean currentUser = (CurrentUserBean)SpringFacesUtil.getManagedBean(PlatformManageBeanConstants.CURRENT_USER_BEAN);
		if (null != currentUser) {
			objMethod = objClass.getMethod("setModifier", new Class[]{com.songqintuan.model.po.SystemUser.class});
			objMethod.invoke(obj, currentUser.getSystemUser());
		}
		} catch (IllegalAccessException e) {
					//TODO        
					} 
		}
	public void setCreatePropertyValue(T obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException{
		try {
		Class<? extends Object> objClass = obj.getClass();
		@SuppressWarnings("unchecked")
		Method objMethod = objClass.getMethod("setCreateTime", new Class[]{Timestamp.class});
		Timestamp nowTime = DateTimeUtil.getCurrentTimestamp();
		objMethod.invoke(obj, new Object[]{nowTime});
		//set update by info
		CurrentUserBean currentUser = (CurrentUserBean)SpringFacesUtil.getManagedBean(PlatformManageBeanConstants.CURRENT_USER_BEAN);
		if (null != currentUser) {
			objMethod = objClass.getMethod("setCreator", new Class[]{com.songqintuan.model.po.SystemUser.class});
			objMethod.invoke(obj, currentUser.getSystemUser());
		}
		} catch (IllegalAccessException e) {
					//TODO        
					} 
		}
	
}
